<template>
  <div :class="className" :style="{height:height,width:width}" />
</template>

<script>
import echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
import resize from '../../../../dashboard/mixins/resize'

const animationDuration = 6000

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '300px'
    }
  },
  data() {
    return {
      chart: null,
      list1: [140, 232, 101, 264, 90, 340, 250],
      list2: [120, 282, 111, 234, 220, 340, 310],
    }
  },
  mounted() {
    this.formartData()
    this.$nextTick(() => {
      this.initChart()
    })
    console.log('this timer',this.timer);
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
    console.log('this timer',this.timer);
    clearInterval(this.timer)
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons1')

      this.chart.setOption({
        // '#80FFA5', '#00DDFF', '#37A2FF',
        color: [ '#FF0087', '#FFBF00'],
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'cross',
            label: {
              backgroundColor: '#6a7985'
            }
          }
        },
        legend: {
          data: ['时间稼动率', '性能稼动率']
        },
        grid: {
          left: '3%',
          right: '4%',
          bottom: '3%',
          containLabel: true
        },
        xAxis: [
          {
            type: 'category',
            boundaryGap: false,
            data: ['03-01', '03-02', '03-03', '03-04', '03-05', '03-06', '03-07'],
            axisLine:{
              show: false,
              onZero: true, //刻度线是否在0刻度上
              lineStyle:{
                color:'#999',
                width:1,//这里是为了突出显示加上的
              }
            },
            axisTick:{
              show: false
            },
            splitLine :{
              show: false
            }
          }
        ],
        yAxis: [
          {
            type: 'value',
            splitLine :{
              show: false
            },
            axisLine:{
              show: false,
              onZero: true, //刻度线是否在0刻度上
              lineStyle:{
                color:'#999',
                width:1,//这里是为了突出显示加上的
              }
            },
            axisTick:{
              show: false
            },
          },
        ],
        series: [
          {
            name: '时间稼动率',
            type: 'line',
            // stack: 'Total',
            smooth: true,
            lineStyle: {
              width: 0
            },
            showSymbol: false,
            areaStyle: {
              opacity: 0.8,
              color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                {
                  offset: 0,
                  color: 'rgb(128, 255, 165)'
                },
                {
                  offset: 1,
                  color: 'rgb(1, 191, 236)'
                }
              ])
            },
            emphasis: {
              focus: 'series'
            },
            data: this.list1
          },
          {
            name: '性能稼动率',
            type: 'line',
            // stack: 'Total',
            smooth: true,
            lineStyle: {
              width: 0
            },
            showSymbol: false,
            areaStyle: {
              opacity: 0.8,
              color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                {
                  offset: 0,
                  color: 'rgb(0, 221, 255)'
                },
                {
                  offset: 1,
                  color: 'rgb(77, 119, 255)'
                }
              ])
            },
            emphasis: {
              focus: 'series'
            },
            data: this.list2
          },
        ]
      })
    },
    // 随机数
    generateRandomInt(min, max) {
      min = Math.ceil(min);
      max = Math.floor(max);
      return Math.floor(Math.random() * (max - min + 1)) + min;
    },

    generateRandomArray(size, min, max) {
      const result = [];
      for (let i = 0; i < size; i++) {
        result.push(this.generateRandomInt(min, max));
      }
      return result;
    },
    formartData(){
      var timer = setInterval(() => {
        const arr = this.generateRandomArray(7, 1, 200)
        const arr1 = this.generateRandomArray(7, 1, 300)
        this.list1 = arr
        this.list2 = arr1
        this.initChart()
      }, 10000)
    }
  }
}
</script>
